package com.tarena.lbs.basic.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.basic.dao.mapper.AreaMapper;
import com.tarena.lbs.basic.dao.repository.AreaRepository;
import com.tarena.lbs.basic.pojo.po.AreaPO;
import com.tarena.lbs.basic.pojo.vo.AreaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.beans.JavaBean;
import java.util.ArrayList;
import java.util.List;

@Repository
@Slf4j
public class
AreaRepositoryImpl implements AreaRepository {
    @Autowired
    private AreaMapper areaMapper;

    @Override
    public List<AreaVO> selectProvinces() {
        List<AreaVO> provincesVO = new ArrayList<>();
        QueryWrapper<AreaPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",0);
//        所有省份
        List<AreaPO>  provincesPo =  areaMapper.selectList(queryWrapper);
        for (AreaPO provincePo : provincesPo){
            log.info("provincePo:{}",provincePo);
            AreaVO provinceVO = new AreaVO();
            List<AreaVO> citiesVO = new ArrayList<>();
            BeanUtils.copyProperties(provincePo,provinceVO);
//            省下的市
            QueryWrapper<AreaPO> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("parent_id",provincePo.getId());
            List<AreaPO> citysPo = areaMapper.selectList(queryWrapper1);
            for (AreaPO cityPO : citysPo){
//
                AreaVO cityVO = new AreaVO();
                List<AreaVO> areasVO = new ArrayList<>();
                BeanUtils.copyProperties(cityPO,cityVO);
//              市辖区
                QueryWrapper<AreaPO> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("parent_id",cityPO.getId());
                List<AreaPO> areasPo = areaMapper.selectList(queryWrapper2);
                for (AreaPO areaPO : areasPo){
//
                    AreaVO areaVO = new AreaVO();
                    BeanUtils.copyProperties(areaPO,areaVO);
                    areasVO.add(areaVO);
//
                }
                cityVO.setChildren(areasVO);
                citiesVO.add(cityVO);
//
            }
            provinceVO.setChildren(citiesVO);
            provincesVO.add(provinceVO);
//
        }

//
        return provincesVO;
    }
}
